package hpu.bitenergy.util;

import java.util.ArrayList;
import java.util.List;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

public class StringHelper {
	public static final String DBNAME = "BitEnergy";

	public static final String EXPERTSCOL = "experts";

	public static final String INTELLIGENCECOL = "intelligence";

	public static final String LIERATURECOL = "literature";

	public static final String PATENT = "patent";

	/**
	 * 01.为 文献和专利去除 空格
	 */
	public static String RemoveKong(String name) {
		name = name.trim();
		return name;
	}

	/**
	 * 02.为 情报 加上1个空格
	 */
	public static String AddOneKong(String name) {
		name = " " + name;
		return name;
	}

	/**
	 * 03.为 专家 加上2个 空格
	 */
	public static String AddTwoKong(String name) {
		name = "  " + name;
		return name;
	}

	public static String GetAuther_name(List<DBObject> authers_name,
			String expert_name) {
		// 提取合作者名字

		String oldname = "", newname ="";
		for (int j = 0; j < authers_name.size(); j++) {
			oldname += authers_name.get(j).get("author_cn").toString();
		}
		String[] arr = oldname.split(";");
		for (int i = 0; i < arr.length; i++) {
			if (newname.contains(arr[i].trim()) == false) {
				newname += arr[i] + ",";//用，号分割
			}
		}
		if (newname.equals(""))
			newname ="没有合作者";
		newname = newname.replace(expert_name,"");// 去除自己
		//newname = newname.replace("   ","");
		return newname;
	}
	
	//得到研究方向，
	@SuppressWarnings("null")
	public static  String  GetKey(List<DBObject> key) {
		int []count =new int [200];
		int sum;
		String oldkey ="", newkey ="",rekey="";
		//全部组合到一块并分词
		for (int j = 0; j < key.size(); j++) {
			oldkey += key.get(j).get("key").toString();
		}		
		String[] oldarr = oldkey.split(";");
		//分出不重复的；
		for(int i=0;i<oldarr.length;i++)
		{
			if(newkey.contains(oldarr[i].trim())==false)
					{
					  newkey += oldarr[i] +";";
					}
		}	
		String [] newarr=newkey.split(";");
		//对比后  加权
		for(int i=0;i<newarr.length;i++)
		{	sum=0;
			for(int j=0;j<oldarr.length;j++)
				{
					if(newarr[i].equals(oldarr[j]))
					{	
						sum++;
					}
				}
			count[i]=sum;
		}
		//对比权值排序并调整下标
		for(int i=0;i<count.length-1;i++)
			for(int j=0;j<count.length-1-i;j++)
			{
				if(count[j]<count[j+1])
				{
					sum=count[j];
					count[j]=count[j+1];
					count[j+1]=sum;
					String temp =newarr[j];
					newarr[j]=newarr[j+1];
					newarr[j+1]=temp;					
				}
			}
		//输出五个
		if(newarr.length>4)
		{
			for(int i=0;i<5;i++)
			{	rekey+=newarr[i].trim()+";";
				System.out.println(count[i]+"");
			}
		}else
		{
			for(int i=0;i<newarr.length;i++)
			{	rekey+=newarr[i].trim()+";";
			System.out.println(count[i]+"");
			}
		}	
		return rekey;

	}
	//关联图
	public static DBObject GetAuther_nameListTest(List<DBObject> authers_name,
			String expert_name) {
		//存储封装好的数据合作者名字
        // List<DBObject> list=new ArrayList<DBObject>();
	 	DBObject db=new BasicDBObject();
        //存关联专家的名字
        List<String> nameList=new ArrayList<String>();
        //01.循环专家信息
        for(DBObject obj : authers_name){
        	//02.拿到关联专家字符串
        	String objName=obj.get("author_cn").toString();
        	if(objName.length()>0){
        	objName=objName.substring(0,objName.length()-1);}
        	
        	//03.拿到每个专家名字
        	String [] names=objName.split(";");
        	for(String name : names){
        		//04.去重：如果包含此专家不存，去掉本人，去掉长度大于1的专家
        		String name1=name.trim();
        		if(nameList.contains(name1)|| name1.length()>10||name1.equals(expert_name)){
        			continue;
        		}
        		nameList.add(name1);
        	}
        }
       
        
        //05.封装数据 :示例 {"老大"：["老三","老二","老四","老五"]}
//        if(nameList.size()==0){
//        	 nameList.add("nobody");
//         }
         db.put(expert_name, nameList);
         //System.out.println(db);
		return db;
	}
	public static List<String> GetAuther_nameList(List<DBObject> authers_name,
			String expert_name) {

        //存关联专家的名字
        List<String> nameList=new ArrayList<String>();
        //01.循环专家信息
        for(DBObject obj : authers_name){
        	//02.拿到关联专家字符串
        	String objName=obj.get("author_cn").toString();
        	if(objName.length()>0){
        	objName=objName.substring(0,objName.length()-1);}
        	
        	//03.拿到每个专家名字
        	String [] names=objName.split(";");
        	for(String name : names){
        		//04.去重：如果包含此专家不存，去掉本人，去掉长度大于1的专家
        		String name1=name.trim();
        		if(nameList.contains(name1)|| name1.length()>10||name1.equals(expert_name)){
        			continue;
        		}
        		nameList.add(name1);
        	}
        }
      
		return nameList;
	}
	// 封装云图数据格式
		public static List<List<Object>> Getyuntukeys(List<DBObject> data) {

			// 分析：返回数据格式为：[[yuan, 2], [yuabdfn, 34]]
			// 01.外层数据
			List<List<Object>> list = new ArrayList<List<Object>>();
			// 02.存取所有的key
			List<String> Allkeys = new ArrayList<String>();
			List<String> Dankeys = new ArrayList<String>();
			
			for (DBObject db : data) {
				// 03.拿到每个key
				String keyStr = db.get("key").toString();
				// 04.消去最后的 ";"
				if (keyStr.length() > 0) {
					keyStr = keyStr.substring(0, keyStr.length() - 1);
					String [] keys=keyStr.split(";");
					//05.取回每个key
					for(String key : keys){
						
						String k=key.trim();
						Allkeys.add(k);
						//存不重复的key
						if(Dankeys.contains(k))
							continue;
						Dankeys.add(k);
					}
				}
			}//外层for循环结束
			System.out.println(Allkeys.size()+"|"+Dankeys.size());
			//06.
			if(Allkeys.size()>0){
				//07.取值,封装数据
	          for(String key : Dankeys){
	        	  int count=3;//(可以控制界面上的字体大小)
	        	  for(String ak : Allkeys){
	        		  if(ak.equals(key)){
	        			  count++;
	        		  }
	        	  }
	        	//08.加入每个数据
	            List<Object> keyCount= new ArrayList<Object>();
	            keyCount.add("\""+key+"\"");
	            keyCount.add(count);
	            list.add(keyCount);
	          }
	          
	          //09.这里就不排序了
	          
				
			}else{
				//10.没有研究方向信息
				List<Object> noIfo= new ArrayList<Object>();
				noIfo.add("暂无信息");
				noIfo.add("10");
				list.add(noIfo);
			}
			
			return list;

		}
}
